# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=openldap
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.6.6
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="OpenLDAP Software is an open source implementation of the Lightweight Directory Access Protocol (only client) (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         #"${MINGW_PACKAGE_PREFIX}-db<6.0.20"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-libsystre"
             "groff")
options=('!makeflags')
license=("The OpenLDAP Public License")
url="https://www.openldap.org/"
source=(https://www.openldap.org/software/download/OpenLDAP/openldap-release/${_realname}-${pkgver}.tgz
        001-backmdb.patch
        002-mingw-build-fixes.patch
        003-symdummy-error.patch)
noextract=(${_realname}-${pkgver}.tgz)
sha256sums=('082e998cf542984d43634442dbe11da860759e510907152ea579bdc42fe39ea0'
            'b462bfab9362cf5cab61d67cd24902a1800fb498a152b681ee182c713b79146c'
            'a1f83d6f58c0371a21a645dec7f30ec7732e6a93f273ea5f89f52acc05f0486c'
            'd697fe51361a5129a5af3ca317031ff6f4ca5ddd247ff53043880660154a4708')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  plain "Extract ${_realname}-${pkgver}.tgz"
  tar -xzf ${_realname}-${pkgver}.tgz || true

  cd ${_realname}-${pkgver}

  # imported from https://github.com/cfengine/buildscripts/blob/master/deps-packaging/openldap/mingw_build_fixes.patch
  apply_patch_with_msg \
    002-mingw-build-fixes.patch

  apply_patch_with_msg \
    001-backmdb.patch \
    003-symdummy-error.patch

  #libtoolize --force --copy
  #AUTOMAKE=/usr/bin/true autoreconf -fi
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  unset CPPFLAGS
  export lt_cv_deplibs_check_method='pass_all'
  CFLAGS+=" -Wno-implicit-int" \
  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --sbindir=${MINGW_PREFIX}/bin \
    --with-tls=openssl \
    --with-cyrus-sasl \
    --enable-modules=yes \
    --disable-slapd

  sed -i "s/#define socklen_t int/\/*#define socklen_t int*\//" include/portable.h
  sed -i "s/#define snprintf _snprintf/\/*#define snprintf _snprintf*\//" include/portable.h
  make depend
  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR=${pkgdir} install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
